function RDFaExporter() {
}

RDFaExporter.prototype.getParentAttribute = function(node, attribute) {
    while (node.tagName != 'HTML' && !node.getAttribute(attribute)) {
        node = node.parentNode;
    }

    return node.getAttribute(attribute);
}

function rdfaViewerGetParentAttribute(node, attribute) {
    while (node.tagName != 'HTML' && !node.getAttribute(attribute)) {
        node = node.parentNode;
    }

    return node.getAttribute(attribute);
}

function rdfaViewerGetParentAttributeOrReplaceAttribute(node, attribute, replaceAttribute) {
    while (node.tagName != 'HTML' && !node.getAttribute(attribute) && !node.getAttribute(replaceAttribute)) {
        node = node.parentNode;
    }

    return node.getAttribute(attribute) || node.getAttribute(replaceAttribute);
}

function createTriple(sub, pre, obj, subTag, preTag, objTag) {
    return {
        subject:{
            tag:subTag,
            value:sub
        },
        predicate:{
            tag:preTag,
            value:pre
        },
        object:{
            tag:objTag,
            value: obj
        }
    }
}

function rdfaViewerLookupRDFa() {
    var defaultSubject = document.URL;
    if(defaultSubject.indexOf('#') >=0) {
        defaultSubject = defaultSubject.substring(0, defaultSubject.indexOf('#'));
    }
    var xpath = '//*[@*[local-name() = "property" or local-name() = "rel" or local-name() = "rev" or local-name() = "typeof"]]'
    var result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    var node = result.iterateNext();
    var data = [];
    var index = 1;
    while (node) {

        var about = node.getAttribute('about');
        var property = node.getAttribute('property');
        var content = node.getAttribute('content') || node.innerHTML;
        var rel = node.getAttribute('rel');
        var href = node.getAttribute('href');
        var rev = node.getAttribute('rev');
        var typeOf = node.getAttribute('typeof');
        if (!about) {
            if (node.tagName == 'LINK' || node.tagName == 'META') {
                if (node.parentNode.tagName == 'HEAD') {
                    about = defaultSubject;
                } else {
                    about = rdfaViewerGetParentAttributeOrReplaceAttribute(node, 'about', 'id');
                }
            } else {
                about = rdfaViewerGetParentAttribute(node, 'about');
            }

            if (!about) {
                about = defaultSubject;
            }
        }
        if(about.indexOf('#') == 0) {
            about = defaultSubject + about;
        }

        if (typeOf) {
            data.push(createTriple(about, 'rdf:type', typeOf, 'about', '', 'typeof'));
            index++;
        }
        if (property) {
            data.push(createTriple(about, property, content, 'about', 'property', 'content'));
            index++;
        }

        if (rel) {
            data.push(createTriple(about, rel, href, 'about', 'rel', 'href'));
            index++;
        }

        if (rev) {
            data.push(createTriple(href, rev, about, 'href', 'rev', 'about'));
            index++;
        }
        node = result.iterateNext();
    }

    for ( var i = 0; i < data.length - 1; i++) {
        if (!data[i].object.value) {
            data[i].object.value = data[i + 1].subject.value;
        }
    }
    return data;
}